// pages/studyWord/everybox/everybox.js
const app = getApp();
let tool = require('../../../utils/tool.js');
const httpUtils = require('../../../utils/httpUtils.js');


let questData = [
  {
    "word": "magazine",//单词
    "etc": "杂志",//释义
    "vedio_url": "https://yixuepai-media.oss-cn-beijing.aliyuncs.com/Public/uploads/england/caution.mp3"//音频路径
  },
  {
    "word": "strawberry",
    "etc": "草莓",
    "vedio_url": "https://yixuepai-media.oss-cn-beijing.aliyuncs.com/Public/uploads/england/caution.mp3"
  },
  {
    "word": "visit",
    "etc": "参观",
    "vedio_url": "http://yxp-media.oss-cn-beijing.aliyuncs.com/video/B/Bible.mp4?Expires=1557473250&OSSAccessKeyId=TMP.AgE4INOMKFKcYAnB3Lcrwga401tXnDfaJC26swTFUYo_2QLdx0BBTd99UyLFADAtAhUAxmvyZSHixeAFVyqZVHkP6fHppaoCFFL1zDgK2sKaTXW8y3V45up0TZ2j&Signature=t%2B71ue7DT0pXCD1iq%2BDhnmMIIkM%3D"
  },
  {
    "word": "finger",
    "etc": "手指",
    "vedio_url": "http://yxp-media.oss-cn-beijing.aliyuncs.com/video/B/Bible.mp4?Expires=1557473250&OSSAccessKeyId=TMP.AgE4INOMKFKcYAnB3Lcrwga401tXnDfaJC26swTFUYo_2QLdx0BBTd99UyLFADAtAhUAxmvyZSHixeAFVyqZVHkP6fHppaoCFFL1zDgK2sKaTXW8y3V45up0TZ2j&Signature=t%2B71ue7DT0pXCD1iq%2BDhnmMIIkM%3D"
  },
  {
    "word": "fruit",
    "etc": "水果",
    "vedio_url": "http://yxp-media.oss-cn-beijing.aliyuncs.com/video/B/Bible.mp4?Expires=1557473250&OSSAccessKeyId=TMP.AgE4INOMKFKcYAnB3Lcrwga401tXnDfaJC26swTFUYo_2QLdx0BBTd99UyLFADAtAhUAxmvyZSHixeAFVyqZVHkP6fHppaoCFFL1zDgK2sKaTXW8y3V45up0TZ2j&Signature=t%2B71ue7DT0pXCD1iq%2BDhnmMIIkM%3D"
  },
  {
    "word": "ship",
    "etc": "船",
    "vedio_url": "http://yxp-media.oss-cn-beijing.aliyuncs.com/video/B/Bible.mp4?Expires=1557473250&OSSAccessKeyId=TMP.AgE4INOMKFKcYAnB3Lcrwga401tXnDfaJC26swTFUYo_2QLdx0BBTd99UyLFADAtAhUAxmvyZSHixeAFVyqZVHkP6fHppaoCFFL1zDgK2sKaTXW8y3V45up0TZ2j&Signature=t%2B71ue7DT0pXCD1iq%2BDhnmMIIkM%3D"
  }
];

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageurl: app.globalData.imageurl,
    okNums: 0,
    errNums: 0,
    selNums: {},
    ansNums: {},
    innerAudioContext: new Object(),
    allWord: '',
    errWord: [],//累计不会的词
    can_notString: "",//不会的词的id字符串
    questData: questData,
    wordID: 0,
    wordLen: 0,
    readTimes: {},
    packageid: "",
    page: 1,//分页 第一页是1
    pageSize: 200,
    hasMoreData: false,
    boxnum: 1, //盒子num
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.data.innerAudioContext = wx.createInnerAudioContext()
    this.data.innerAudioContext.autoplay = false;
    this.data.innerAudioContext.loop = false;
    //获取到传过来的包id
    this.data.packageid = options.packageid;
    //获取到传过来的盒子num
    let boxnum = options.boxNum;
    that.setData({
      boxnum: boxnum
    })

    let unitnum = boxnum - 1;
    //设置title
    if (unitnum == 0) {
      wx.setNavigationBarTitle({
        title: '不会的词',
      })
    } else {
      wx.setNavigationBarTitle({
        title: 'Unit ' + unitnum,
      })
    }
    //获取盒子单词
    that.getBoxWords(boxnum);
  },

  //获取盒子里的单词
  getBoxWords: function (boxnum) {
    console.log("调查看某个盒子单词的接口，盒子：" + boxnum)
    let that = this;
    wx.showLoading({
      title: '',
      mask: true
    })
    httpUtils.getBoxWords({
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      package_id: that.data.packageid,
      square_type: boxnum,
      page: that.data.page,
    }).then((res) => {
      var questData = that.data.questData;
      var list = res.data;
      if (that.data.page == 1) {
        questData = []
      }
      if (list.length < that.data.pageSize) {
        that.setData({
          questData: questData.concat(list),
          hasMoreData: false
        })
      } else {
        that.setData({
          questData: questData.concat(list),
          hasMoreData: true,
          page: that.data.page + 1,
        })
      }
    });
  },

  clickTxt: function (e) {
    this.data.innerAudioContext.stop();
    let temWrod = e.currentTarget.dataset.words;
    let temNum = this.data.selNums;
    // let audiosrc = app.globalData.voicepath + temWrod + ".mp3";
    let audiosrc=e.currentTarget.dataset.audios;
    this.data.innerAudioContext.src = audiosrc;
    this.data.innerAudioContext.play();
    // if (temNum[temWrod] === undefined) {
    //   temNum[temWrod] = 2;
    //   this.setData({
    //     selNums: temNum,
    //     word1Cls: 'xhjc_text3_2'
    //   });
    // } else {
    //   let rTimes = this.data.readTimes;
    //   rTimes[temWrod] = 1;
    //   if (temNum[temWrod] == 1) {
    //     temNum[temWrod] = 2;
    //   } else {
    //     temNum[temWrod] = 1;
    //   }
    //   this.setData({
    //     selNums: temNum,
    //     readTimes: rTimes
    //   });
    // }

    var isHas = false;//是否点击过 false没有点击过
    for (var key in temNum) {//0正常样式1英文变粗 2英文变粗出中文
      if (key == temWrod) {
        if (temNum[key] < 2) {
          temNum[key]++;
        } else {
          temNum[key] = 1;
        }
        isHas = true;
      } else {
        temNum[key] = 0;
      }
    }
    if (!isHas) {
      temNum[temWrod] = 1;
    }
    this.setData({
      selNums: temNum,
    });

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("onReachBottom方法触发了")
    var that = this;
    let boxnum =that.data.boxnum;
    if (that.data.hasMoreData) {
      that.getBoxWords(boxnum);
      console.log("请求下一页数据")
    } else {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none'
      })
    }
  },

})